<?php


namespace App\models;


use Consts\RedisKey;
use Helper\Output;
use Helper\Mysql;
use Helper\Redis;
use Helper\Reg;

class Session
{


    /**
     * 推送消息
     * @param $sessid  会话ID
     * @param $send_uid 发送者ID
     * @param $uid  接受者ID
     * @param $content 消息内容
     * @param int $type  消息类型：1文本，2图片，3视频
     * @param int $send_user_type 发送者类型：1客服。2用户
     * @return bool|false|string
     * @throws \Exception
     */
    static public function pushChatMessage($sessid,$send_uid,$uid,$content,$statusCode = 2000,$type = 1 ,$send_user_type = 1){

        $data = [
            'send_uid'          => $send_uid,
            'accept_uid'        => $uid,
            'content'           => $content,
            'type'              => $type,
            'session_id'        => $sessid,
            'create_at'         => time(),
            'send_user_type'    => 1,
        ];

        Mysql::getInstance()->insert('user_chat_record',$data);
        $data['id'] = Mysql::getInstance()->getInsertId();
        if($send_user_type == 1){
            $acceptfd = FdManager::getFdByUid($uid);
        }else{
            $acceptfd = FdManager::getFdByAuid($send_uid);
        }

        if(Reg::getInstance()->get('server')->isEstablished($acceptfd)){

            Reg::getInstance()->get('server')->push($acceptfd,Output::json($statusCode,$data));
        }

        return  true;
    }

}